﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using BE;
using BE.SearchCriteria;

namespace DAL
{
    public class RubroDAL : BaseDAL 
    {
        public List<RubroBE> SeleccionarPorParametros(BaseSC pFiltroSC, int pPageSize, int? pPageNum, ref int pCountReg)
        {
            RubroSC wFiltroSC = (RubroSC)pFiltroSC;
            if (wFiltroSC == null)
                wFiltroSC = new RubroSC();

            List<RubroBE> listaDeRubroBE = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@IdRubro", wFiltroSC.IdRubro));
                cmd.Parameters.Add(new SqlParameter("@Descripcion", wFiltroSC.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@pagesize", pPageSize));
                cmd.Parameters.Add(new SqlParameter("@pagenum", pPageNum));

                SqlParameter p = new SqlParameter();
                p.ParameterName = "@PageCount";
                p.Direction = ParameterDirection.Output;
                p.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(p);

                cmd.CommandText = "Rubros_GetAllPaginated";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    listaDeRubroBE = new List<RubroBE>();

                    while (reader.Read())
                    {

                        RubroBE oRubroBE = ConstruirRubro(reader);

                        listaDeRubroBE.Add(oRubroBE);
                    }
                }
                reader.Close();
                pCountReg = (Int32)cmd.Parameters["@PageCount"].Value;

            }
            return listaDeRubroBE;
        }

        public RubroBE SeleccionarPorId(RubroBE oRubroBE)
        {
            RubroBE oRubro = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@IdRubro", oRubroBE.IdRubro));
                cmd.CommandText = "Rubros_GetById";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        oRubro = ConstruirRubro(reader);
                    }
                }

            }
            return oRubro;
        }

        private RubroBE ConstruirRubro(SqlDataReader reader)
        {
            RubroBE oRubroBE = new RubroBE();
            Object[] values = new Object[reader.FieldCount];

            reader.GetValues(values);

            oRubroBE.IdRubro = Int32.Parse(reader["IdRubro"].ToString());
            oRubroBE.Descripcion = reader["Descripcion"].ToString();

            return oRubroBE;
        }

        public void Insertar(RubroBE Rubro)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Rubros_i";
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", Rubro.Descripcion));

                    SqlParameter p = new SqlParameter();
                    p.ParameterName = "@IdRubro";
                    p.Direction = ParameterDirection.Output;
                    p.SqlDbType = SqlDbType.Int;
                    cmd.Parameters.Add(p);

                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    Rubro.IdRubro = (Int32)cmd.Parameters["@IdRubro"].Value;
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Modificar(RubroBE pRubro)
        {
            try
            {
                using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Rubros_u";
                    cmd.Parameters.Add(new SqlParameter("@IdRubro", pRubro.IdRubro));
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pRubro.Descripcion));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            { throw; }
        }

        public void Delete(RubroBE pRubro)
        {
            try
            {
                using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Rubros_d";
                    cmd.Parameters.Add(new SqlParameter("@IdRubro", pRubro.IdRubro));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            { throw; }
        }

    }
}
